package com.hotel.controllers;

import com.hotel.entitys.Dept;
import com.hotel.entitys.RestPOJO;
import com.hotel.services.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags="员工管理接口")//tags:说明信息
public class DeptController {
    @Autowired
    private DeptService deptService;


    @ApiOperation(
            value = "获得所有部门信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //获得所有部门信息
    @GetMapping("/depts")
    public RestPOJO getAll(String keyWord) {
        RestPOJO restPOJO = new RestPOJO();
        List<Dept> l = deptService.getAllDept(keyWord);
        if (l.size() > 0) {
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(l);
        } else {
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    //删除部门信息

    @ApiOperation(
            value = "根据主键id删除部门信息",
            httpMethod = "DELETE",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @DeleteMapping("/depts/{a}")
    public RestPOJO delOne(@PathVariable("a") Integer dept_id) {
        RestPOJO restPOJO = new RestPOJO();
        int count = deptService.deleteDeptBydept_Id(dept_id);
        if (count > 0) {
            restPOJO.setMessage("删除成功");
            restPOJO.setStatus(1000);
        } else {
            restPOJO.setMessage("删除失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    //增加

    @ApiOperation(
            value = "增加员工",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping("/depts")
    public RestPOJO save(@RequestBody Dept dept) {
        RestPOJO restPOJO = new RestPOJO();
        int count = deptService.addDept(dept);
        if (count > 0) {
            restPOJO.setMessage("增加成功");
            restPOJO.setStatus(1000);
        } else {
            restPOJO.setMessage("增加失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    // 获得单个部门信息

    @ApiOperation(
            value = "根据主键id获得单个部门信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/depts/upd/{id}")
    public RestPOJO getSingleDept(@PathVariable("id") Integer dept_id){
        RestPOJO restPOJO = new RestPOJO();
        Dept dept = deptService.getOneDept(dept_id);
        restPOJO.setData(dept);
        return restPOJO;
    }
    //修改
    @ApiOperation(
            value = "修改部门信息",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PutMapping("/depts")
    public RestPOJO updSave(@RequestBody Dept dept) {
        RestPOJO restPOJO = new RestPOJO();
        int count = deptService.updateDept(dept);
        if (count > 0) {
            restPOJO.setMessage("修改成功");
            restPOJO.setStatus(1000);
        } else {
            restPOJO.setMessage("修改失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


}